<template>
  <div class="admin-layout">
    <!-- 顶部标题栏 -->
    <header class="admin-header">
      <div class="logo">
        <h1>映像婚纱约拍管理系统</h1>
      </div>
      <div class="user-info">
        <el-dropdown trigger="click" @command="handleCommand">
          <div class="user-avatar-info">
            <el-avatar :size="40" :src="userStore.userDetail?.avatar">
              {{ userStore.username?.charAt(0).toUpperCase() }}
            </el-avatar>
            <span class="username">{{ userStore.userDetail?.nickname || userStore.username }}</span>
            <el-icon><ArrowDown /></el-icon>
          </div>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="logout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </header>

    <div class="admin-container">
      <!-- 左侧菜单栏 -->
      <aside class="admin-sidebar">
        <el-menu
          :default-active="activeMenu"
          class="admin-menu"
          :router="true"
          :collapse="isCollapse"
        >
          <div class="menu-toggle" @click="toggleMenu">
            <el-icon v-if="isCollapse"><Expand /></el-icon>
            <el-icon v-else><Fold /></el-icon>
          </div>

          <el-menu-item index="/admin/profile">
            <el-icon><User /></el-icon>
            <template #title>个人资料</template>
          </el-menu-item>

          <el-sub-menu v-if="hasAdminAccess" index="1">
            <template #title>
              <el-icon><UserFilled /></el-icon>
              <span>用户管理</span>
            </template>
            <el-menu-item v-if="isSuperAdmin" index="/admin/customers">客户管理</el-menu-item>
            <el-menu-item index="/admin/photographers">摄影师管理</el-menu-item>
            <el-menu-item v-if="isSuperAdmin" index="/admin/admins">管理员管理</el-menu-item>
          </el-sub-menu>

          <el-sub-menu v-if="hasAdminAccess" index="2">
            <template #title>
              <el-icon><Calendar /></el-icon>
              <span>预约管理</span>
            </template>
            <el-menu-item index="/admin/bookings">预约列表</el-menu-item>
          </el-sub-menu>

          <el-sub-menu v-if="hasAdminAccess" index="3">
            <template #title>
              <el-icon><ShoppingCart /></el-icon>
              <span>订单管理</span>
            </template>
            <el-menu-item index="/admin/orders">订单列表</el-menu-item>
          </el-sub-menu>

          <el-sub-menu v-if="hasAdminAccess" index="4">
            <template #title>
              <el-icon><Picture /></el-icon>
              <span>内容管理</span>
            </template>
            <el-menu-item index="/admin/samples">样片管理</el-menu-item>
            <el-menu-item index="/admin/packages">套餐管理</el-menu-item>
            <!-- <el-menu-item index="/admin/dresses">婚纱管理</el-menu-item> -->
            <el-menu-item index="/admin/banners">Banner管理</el-menu-item>
          </el-sub-menu>

          <el-menu-item index="/admin/reviews">
            <el-icon><ChatDotRound /></el-icon>
            <template #title>评价管理</template>
          </el-menu-item>
        </el-menu>
      </aside>

      <!-- 右侧内容区域 -->
      <main class="admin-content">
        <div class="content-body">
          <router-view />
        </div>
      </main>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
  User,
  UserFilled,
  Calendar,
  ShoppingCart,
  Picture,
  ChatDotRound,
  ArrowDown,
  Expand,
  Fold,
} from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()

// 菜单折叠状态
const isCollapse = ref(false)
const toggleMenu = () => {
  isCollapse.value = !isCollapse.value
}

// 当前激活的菜单
const activeMenu = computed(() => route.path)

// 当前路由的元信息
const currentRouteMeta = computed(() => route.meta)

// 判断用户权限
const hasAdminAccess = computed(() => {
  const localRole = localStorage.getItem('userRole')
  return localRole === 'admin' || localRole === 'super_admin'
})

const isSuperAdmin = computed(() => {
  return localStorage.getItem('userRole') === 'super_admin'
})

// 处理下拉菜单命令
const handleCommand = (command: string) => {
  if (command === 'profile') {
    router.push('/profile/info')
  } else if (command === 'logout') {
    ElMessageBox.confirm('确定要退出登录吗?', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    })
      .then(() => {
        userStore.clearUserInfo()
        router.push('/login')
        ElMessage.success('已退出登录')
      })
      .catch(() => {
        // 取消退出
      })
  }
}

// 检查用户权限
onMounted(() => {
  if (!userStore.isLoggedIn) {
    router.push('/login')
    ElMessage.warning('请先登录')
  }
})
</script>

<style scoped>
.admin-layout {
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}

.admin-header {
  height: 60px;
  background-color: #001529;
  color: white;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  z-index: 1000;
}

.logo h1 {
  margin: 0;
  font-size: 20px;
  font-weight: 600;
}

.user-info {
  display: flex;
  align-items: center;
}

.user-avatar-info {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.username {
  margin: 0 10px;
  font-size: 14px;
}

.admin-container {
  display: flex;
  flex: 1;
  overflow: hidden;
}

.admin-sidebar {
  background-color: #f5f7fa;
  transition: width 0.3s;
  box-shadow: 2px 0 6px rgba(0, 21, 41, 0.1);
  z-index: 999;
}

.admin-menu {
  height: 100%;
  border-right: none;
}

.admin-menu:not(.el-menu--collapse) {
  width: 220px;
}

.menu-toggle {
  height: 50px;
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  border-bottom: 1px solid #e6e6e6;
}

.admin-content {
  flex: 1;
  overflow-y: auto;
  background-color: #f0f2f5;
}

.content-header {
  margin-bottom: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.content-header h2 {
  margin: 0;
  font-size: 20px;
  font-weight: 500;
}

.content-body {
  background-color: white;
  border-radius: 4px;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
}
</style>
